package com.tang.web_standard.global.response.handler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.SmartLifecycle;
import org.springframework.stereotype.Component;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author tangyuan2meicloud.com
 * @DATE 2021/9/16 19:53
 * @Desc
 * @Remarks
 */
@Component
public class ResponseHandlerWrapperInit implements SmartLifecycle {

    @Autowired
    private RequestMappingHandlerAdapter adapter;

    @Override
    public void start() {
        List<HandlerMethodReturnValueHandler> returnValueHandlers = adapter.getReturnValueHandlers();
        List<HandlerMethodReturnValueHandler> wrappers = new ArrayList<>();
        for (HandlerMethodReturnValueHandler returnValueHandler : returnValueHandlers) {
            wrappers.add(new ResponseBodyWrapper(returnValueHandler));
        }
        adapter.setReturnValueHandlers(wrappers);
    }

    @Override
    public void stop() {

    }

    @Override
    public boolean isRunning() {
        return false;
    }
}
